package thread;

/**
 * 静态方法的同步
 * 当为一个静态方法添加synchronized,那么该方法一定具有同步效果
 */
public class SyncDemo3 {
    public static void main(String[] args) {
//        Boo b1 = new Boo();
//        Boo b2 = new Boo();
//        Thread t1 = new Thread(()->b1.doSome());
//        Thread t2 = new Thread(()->b2.doSome());

        Thread t1 = new Thread(()->Boo.doSome());
        Thread t2 = new Thread(()->Boo.doSome());

        t1.start();
        t2.start();
    }
}

class Boo{
//    public synchronized static void doSome(){
    public static void doSome(){
        synchronized (Boo.class) {//静态方法上使用的同步监视器对象就是当前类的类对象：类名.class
            try {
                Thread t = Thread.currentThread();
                System.out.println(t + "正在执行doSome方法");
                Thread.sleep(5000);
                System.out.println(t + "执行doSome方法完毕");
            } catch (InterruptedException e) {
            }
        }
    }
}